package dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;

import model.Hardware;

public class HardwareDAO extends PersistenciaDAO {
	
	public Collection<HashMap<String, Object>> buscarDadosHardwareBrowse() throws SQLException {

		Collection<HashMap<String, Object>> result = new ArrayList<HashMap<String,Object>>();
		Statement sta = connection.createStatement();
		ResultSet elements = sta.executeQuery(
				" SELECT h.cd_hardware as codigo, "+
				"  		h.ds_hardware as descricao "+
				" FROM quiosque.hardware h "+
				" ORDER BY h.cd_hardware ");
		
		while (elements.next()) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("codigo", elements.getInt("codigo"));
			map.put("descricao", elements.getString("descricao"));
			result.add(map);
		}
		return result;
	}

	public Hardware buscarHardwarePorId(int id) throws SQLException, Exception {
		Hardware result = null;
		Statement sta = connection.createStatement();
		ResultSet elements = sta
				.executeQuery("SELECT * FROM quiosque.hardware WHERE cd_hardware = " + id);

		while (elements.next()) {
			result = montarHardware(elements);
		}
		return result;
	}

	public Hardware buscarHardwarePorDescricao(String descricao) throws SQLException, Exception {
		Hardware result = null;
		Statement sta = connection.createStatement();
		ResultSet elements = sta
				.executeQuery("SELECT * FROM quiosque.hardware WHERE ds_hardware = '" + descricao +"'");

		while (elements.next()) {
			result = montarHardware(elements);
		}
		return result;
	}

	
	public Hardware montarHardware(ResultSet elements) throws SQLException, Exception {
		Hardware hard = new Hardware();
		hard.setId(elements.getInt("cd_hardware"));
		hard.setDescricao(elements.getString("ds_hardware"));
		return hard;
	}

	public Collection<Hardware> buscarHardwarePorComputador(int idComputador) throws SQLException, Exception {
		Collection<Hardware> hardwares = new ArrayList<Hardware>();

		Statement sta = connection.createStatement();
		ResultSet elements = sta
		.executeQuery("SELECT * FROM computadorhardware WHERE cd_computador = "	+ idComputador);

		while (elements.next()) {
			Hardware hardware = buscarHardwarePorId(elements.getInt("cd_hardware"));
			hardwares.add(hardware);
		}

		return hardwares;
	}

	public Collection<Hardware> buscarTodos() throws SQLException, Exception {
		Collection<Hardware> result = new ArrayList<Hardware>();
		Statement sta = connection.createStatement();
		ResultSet elements = sta
				.executeQuery("SELECT * FROM quiosque.hardware ");

		while (elements.next()) {
			Hardware hardware = buscarHardwarePorId(elements.getInt("cd_hardware"));
			result.add(hardware);
		}
		return result;
	}

}
